package com.ltu.util.search;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;

import com.ltu.util.datetime.DateUtils;


/**
 * @author jarlun
 */
public class SearchFilter {

	/**
	 * 字段名
	 */
	public String fieldName;
	/**
	 * 字段类型
	 */
	public String fieldType;
	/**
	 * 字段值
	 */
	public Object value;
	/**
	 * 操作符
	 */
	public Operator operator;

	public SearchFilter(String fieldType, String fieldName, Operator operator,
			Object value) {
		this.fieldType = fieldType;
		this.fieldName = fieldName;
		this.operator = operator;
		this.value = value;
	}

	/**
	 * searchParams中key的格式为OPERATOR_FIELDNAME
	 */
	public static Map<String, SearchFilter> parse(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		Object value = null;
		int _v = 0;
		for (Entry<String, Object> entry : searchParams.entrySet()) {
			// 过滤掉空值
			String key = entry.getKey();
			if (entry.getValue() == null) {
				continue;
			}
			String _temp = entry.getValue().toString();
			if (StringUtils.isBlank(_temp)) {
				continue;
			}

			// 拆分operator与fieldType,filedName
			String[] keys = StringUtils.split(key, "_");
			if (keys.length != 3) {
				throw new IllegalArgumentException(key + " is not a valid filter key， eg: EQ_int_userName");
			}

			String fieldType = keys[1];
			String filedName = keys[2];
			if ("Int".equals(fieldType)) {
				value = Integer.parseInt(_temp);
			} else if ("Long".equals(fieldType)) {
				value = Long.parseLong(_temp);
			} else if ("Decimal".equals(fieldType)) {
				value = new BigDecimal(_temp);
			} else if ("Boolean".equals(fieldType)) {
				try {
					_v = Integer.parseInt(_temp);
				} catch (NumberFormatException ignore) {
				}
				if (_v > 0 || "true".equalsIgnoreCase(_temp)) {
					value = Boolean.TRUE;
				} else {
					value = Boolean.FALSE;
				}
			} else if ("Date".equals(fieldType)) {
				value = DateUtils.parse(_temp, DateUtils.DATE_TIME_FORMAT);
			} else {
				value = entry.getValue();
			}
			// 创建searchFilter
			filters.put(key, new SearchFilter(fieldType, filedName, Operator.valueOf(keys[0]),
					value));
		}

		return filters;
	}
	
	/**
	 * searchParams中key的格式为OPERATOR_FIELDNAME,传0或者null都不拼
	 */
	public static Map<String, SearchFilter> parseWithoutZN(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
		Object value = null;
		int _v = 0;
		for (Entry<String, Object> entry : searchParams.entrySet()) {
			// 过滤掉空值，0
			String key = entry.getKey();
			if (entry.getValue() == null||entry.getValue().toString().equals("0")) {
				continue;
			}
			String _temp = entry.getValue().toString();
			if (StringUtils.isBlank(_temp)) {
				continue;
			}

			// 拆分operator与fieldType,filedName
			String[] keys = StringUtils.split(key, "_");
			if (keys.length != 3) {
				throw new IllegalArgumentException(key + " is not a valid filter key， eg: EQ_int_userName");
			}

			String fieldType = keys[1];
			String filedName = keys[2];
			if ("Int".equals(fieldType)) {
				value = Integer.parseInt(_temp);
			} else if ("Long".equals(fieldType)) {
				value = Long.parseLong(_temp);
			} else if ("Decimal".equals(fieldType)) {
				value = new BigDecimal(_temp);
			} else if ("Boolean".equals(fieldType)) {
				try {
					_v = Integer.parseInt(_temp);
				} catch (NumberFormatException ignore) {
				}
				if (_v > 0 || "true".equalsIgnoreCase(_temp)) {
					value = Boolean.TRUE;
				} else {
					value = Boolean.FALSE;
				}
			} else if ("Date".equals(fieldType)) {
				value = DateUtils.parse(_temp, DateUtils.DATE_TIME_FORMAT);
			} else {
				value = entry.getValue();
			}
			// 创建searchFilter
			filters.put(key, new SearchFilter(fieldType, filedName, Operator.valueOf(keys[0]),
					value));
		}

		return filters;
	}

}
